import { parseTimeDot } from "@/utils/utils.js";
import { getListInfo } from "@/static/js/message.js"

export default {
  data() {
    return {
      parseTimeDot,
      showMore: false,
      createTime: 0,
      title: '',
      content: '',
      num: 0,
      isTop: 0,
      // 更多菜单
      menuList: [{
        icon: '/static/img/share/wx.png',
        name: '微信好友',
        remark: '',
        url: '',
        handler: 'shareTo'
      }, {
        icon: '/static/img/share/pyq.png',
        name: '朋友圈',
        remark: '',
        url: '',
        handler: 'shareToTimeline'
      }, {
        icon: '/static/img/share/copyLink.png',
        name: '复制链接',
        remark: '',
        url: '',
        handler: 'copyLink'
      }],
    }
  },
  onLoad(e) {
    if (e.id) {
      this.id = e.id
      this.getData()
    } else {
      uni.showToast({
        title: '数据错误即将返回上一页',
        icon: 'error'
      })
      setTimeout(() => {
        uni.switchTab({
          url: '/pages/message/affiche/index'
        })
      }, 2000)
    }
  },
  methods: {
    async getData() {
      const res = await getListInfo({
        id: this.id
      })
      if (res.code == 1) {
        this.content = res.data.content
        this.createTime = res.data.createtime
        this.title = res.data.title
        this.isTop = res.data.is_top
        this.num = res.data.brows
      }
    },

    navigate(e) {
      this.download(e.href)
    },

    //分享到微信好友
    shareTo() {
      const share_url = `${this.$shareBaseUrl.server}/pages/details/myMessage?id=${this.id}`;
      uni.share({
        provider: "weixin",
        scene: "WXSceneSession",
        type: 0,
        href: share_url,
        title: this.title,
        summary: this.getHtmlText(),
        imageUrl: "/static/logo-min.png",
        success: (res) => {
          console.log("success:", res);
          if (callback) {
            callback();
          }
          this.showMore = false;
        },
        fail: (err) => {
          console.log("fail:", err);
        }
      });
    },
    //分享到微信朋友圈
    shareToTimeline() {
      const share_url = `${this.$shareBaseUrl.server}/pages/details/myMessage?id=${this.id}`;
      uni.share({
        provider: "weixin",
        scene: "WXSceneTimeline",
        type: 0,
        href: share_url,
        title: this.title,
        summary: this.getHtmlText(),
        imageUrl: "/static/logo-min.png",
        success: (res) => {
          if (callback) {
            callback();
          }
          this.showMore = false;
        },
        fail: (err) => {
          console.log("fail:", err);
        }
      });
    },
    //复制链接
    copyLink() {
      uni.setClipboardData({
        data: `${this.$shareBaseUrl.server}/pages/details/myMessage?id=${this.id}`,
        success: () => {
          uni.showToast({
            title: '链接复制成功'
          });
          this.showMore = false;
        }
      });
    },
    menuClick(item) {
      if (item.handler) {
        this[item.handler]()
      } else if (item.url) {
        this.$tools.href(item.url)
      }
    },

    go(url) {
      uni.navigateTo({
        url: url,
        fail: function () {
          uni.reLaunch({
            url: url
          })
        }
      })
    },
    //获取富文本内容
    getHtmlText() {
      return str = this.content.replace(/<[^<>]+>/g, "").replace(/&nbsp;/gi, "");
    },
    // 分享弹出弹窗
    actionClick() {
      this.showMore = true;
    },

  }
}